package com.compass.mobile.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.BaseGridView
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.databinding.FragmentCaseItemBinding
import com.compass.mobile.framework.base.BaseVBFragment
import com.compass.mobile.framework.constant.MY_ORDER
import com.compass.mobile.framework.constant.NURSE_ORDER
import com.compass.mobile.framework.constant.YD_USER_ID
import com.compass.mobile.framework.ext.gone
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.ext.visible
import com.compass.mobile.framework.utils.FocusViewUtils
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.NursingOrder
import com.compass.mobile.presenters.NurseOrderPresenter
import com.compass.mobile.ui.dialog.CodeDialog
import com.compass.mobile.ui.viewModel.NursingOrderViewModel
import com.compass.mobile.widget.MyBridgeAdapter

/**
 * Description： 护理订单
 * Date:2024/6/26 20:30
 */
class NursingOrderFragment : BaseVBFragment<FragmentCaseItemBinding>() {
    private val nursingOrderViewModel: NursingOrderViewModel by viewModels()
    private lateinit var arrayObjectAdapter: ArrayObjectAdapter
    private var orders: MutableList<NursingOrder> = mutableListOf()
    private var mPage = 1
    private var isHaveNext = true
    private var type: String = ""
    override fun initView(view: View, savedInstanceState: Bundle?) {
        specialColumn()

        FocusViewUtils.scaleView(mBinding?.tvPrevious, 1)
        FocusViewUtils.scaleView(mBinding?.tvNextPage, 1)

    }

    fun setSelect(type: String) {
        showLoading()
        this.type = type
        mPage = 1
        arrayObjectAdapter.clear()
        nursingOrderViewModel.getOrderNurse( type, mPage)

    }

    override fun initData() {
        nursingOrderViewModel.nurseOrdersLiveData.observe(this) {
            dismissLoading()
            if (mPage == 1) {
                if (it.isNullOrEmpty()) {
                    isHaveNext(0)
                    mBinding?.emptyView?.visible()
                    mBinding?.llBelow?.gone()
                } else {
                    isHaveNext(it.size)
                    mBinding?.emptyView?.gone()
                    orders = it
                    arrayObjectAdapter.clear()
                    arrayObjectAdapter.addAll(0, orders)
                }
            } else {
                if (it.isNullOrEmpty()) {
                    isHaveNext(0)
                    mPage-- //恢复上一次的page
                    showToast("没有下一页了")
                } else {
                    isHaveNext(it.size)
                    orders = it
                    arrayObjectAdapter.clear()
                    arrayObjectAdapter.addAll(0, orders)
                }
            }
        }

    }

    private fun isHaveNext(number: Int) {
        if (number < 10) {
            isHaveNext = false
        } else {
            mBinding?.llBelow?.visible()
            isHaveNext = true
        }
    }


    override fun intListener() {
        mBinding?.tvPrevious?.onClick {
            if (mPage > 1) {
                mPage--
                nursingOrderViewModel.getOrderNurse( type, mPage)
            } else {
                showToast("没有上一页了")
            }
        }

        mBinding?.tvNextPage?.onClick {
            if (isHaveNext) {
                mPage++
                nursingOrderViewModel.getOrderNurse( type, mPage)
            } else {
                showToast("没有下一页了")
            }
        }
    }


    private val onItemClickListener: OnItemClickListener = object : OnItemClickListener {
        override fun onItemClick(position: Int, view: View) {
            val item = orders[position]
            val oderCodeDialog = CodeDialog.Builder(
                requireActivity(),
                NURSE_ORDER + UserInfoManager.getString(YD_USER_ID)+"&type=0"
            )
            oderCodeDialog.show()


        }
    }



    @SuppressLint("RestrictedApi")
    fun specialColumn() {

        mBinding?.caseItem?.horizontalSpacing = dpToPx(10)
        mBinding?.caseItem?.verticalSpacing = dpToPx(10)

        arrayObjectAdapter = ArrayObjectAdapter(NurseOrderPresenter())
        val itemBridgeAdapter = MyBridgeAdapter(
            requireContext(),
            arrayObjectAdapter, onItemClickListener
        )

        mBinding?.caseItem?.setAdapter(itemBridgeAdapter)
        mBinding?.caseItem?.setFocusScrollStrategy(BaseGridView.FOCUS_SCROLL_ITEM)
        arrayObjectAdapter.addAll(0, orders)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_NONE, false
        )
    }
}